﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MVC_Project.Api.Models.ManageGoods;
using MVC_Project.Bll.Systems;
using MVC_Project.Model;
using MVC_Project.Bll.Goods;
using MVC_Project.Api.Models.MobileGoods;
using MVC_Project.Api.Models;
using MVC_Project.Api.Models.Systems;
using System.Drawing;

namespace MVC_Project.Api.Areas.Goods
{
    public class GoodsController : ApiController
    {
        GoodsBll bll = new GoodsBll();
        // GET: api/Goods
        public PageList<EditGood> Get(int page, int size, string name = "")
        {
            var specs = new GoodSpecKeyBll().Query().ToList();

            int count = 0;

            //分页查询数据
            var users = bll.GetGoodsPage(page, size, name, out count);

            //使用Linq 处理数据格式
            var data = from gd in users
                       select new EditGood()
                       {
                           id = gd.id,
                           name = gd.name,
                           content = gd.content,
                           images = gd.images,
                           use_property = gd.use_property,
                           unit = gd.unit,
                           price = gd.price,
                           sales = gd.sales,
                           tid = gd.tid,
                           typename = gd.GoodType.name,
                           property = from sp in specs
                                      join pp in gd.GoodPropertyKey on sp.id equals pp.gskid into temp
                                      from tmp in temp.DefaultIfEmpty()
                                      select new EditValue()
                                      {
                                          id = tmp==null?0: tmp.id,
                                          name = sp.name,
                                          gskid = sp.id,
                                          check = tmp == null ? false : true,
                                          values = tmp != null ? from pv in tmp.GoodPropertyValue
                                                                 select pv.GoodSpecValue.value:null
                                      }
                       };

            var list = data.ToList();

            PageList<EditGood> re = new PageList<EditGood>();
            re.code = 1;
            re.msg = "ok";
            re.count = count;
            re.data = list;
            re.page = page;
            re.size = size;

            return re;
        }

        // GET: api/Goods/5
        public EditGood Get(int id)
        {
            var specs = new GoodSpecKeyBll().Query();

            var goods = bll.Query().Where(g => g.id == id);
            //使用Linq 处理数据格式
            var data = from gd in goods
                       select new EditGood()
                       {
                           id = gd.id,
                           name = gd.name,
                           content = gd.content,
                           images = gd.images,
                           use_property = gd.use_property,
                           unit = gd.unit,
                           price = gd.price,
                           tid = gd.tid,
                           property = from sp in specs
                                      join pp in gd.GoodPropertyKey on sp.id equals pp.gskid into temp
                                      from tmp in temp.DefaultIfEmpty()
                                      select new EditValue()
                                      {
                                          id = tmp == null ? 0 : tmp.id,
                                          name = sp.name,
                                          gskid = sp.id,
                                          check = tmp == null ? false : true,
                                          values = tmp != null ? from pv in tmp.GoodPropertyValue
                                                                 select pv.GoodSpecValue.value : null
                                      }
                       };

            return data.FirstOrDefault();
        }

        // POST: api/Goods
        public Result Post([FromBody] CreateGood value)
        {
            Good gd = new Good();
            gd.name = value.name;
            gd.price = value.price;
            gd.content = value.content;
            gd.unit = value.unit;
            gd.tid = value.tid;
            gd.images = value.images;
            gd.use_property = value.use_property;
            gd.GoodPropertyKey = new List<GoodPropertyKey>() { };

            foreach (ProValue pv in value.property)
            {
                var listvalue = new List<GoodPropertyValue>() { };
                gd.GoodPropertyKey.Add(new GoodPropertyKey()
                {
                    gskid = pv.gskid,
                    GoodPropertyValue = listvalue
                });

                foreach (int vid in pv.values)
                {
                    listvalue.Add(new GoodPropertyValue()
                    {
                        gsvid = vid
                    });
                }
            }

            int num = bll.Add(gd);

            Result res = new Result();
            res.code = num > 0 ? 1 : -1;
            res.msg = num > 0 ? "添加成功" : "添加失败";


            return res;
        }

        // PUT: api/Goods/5
        public Result Put(int id, [FromBody] CreateGood value)
        {
          
            Good gd = new Good();
            gd.id = id;
            gd.name = value.name;
            gd.price = value.price;
            gd.content = value.content;
            gd.unit = value.unit;
            gd.tid = value.tid;
            gd.images = value.images;
            gd.use_property = value.use_property;
            gd.GoodPropertyKey = new List<GoodPropertyKey>() { };

            foreach (ProValue pv in value.property)
            {
                var listvalue = new List<GoodPropertyValue>() { };
                gd.GoodPropertyKey.Add(new GoodPropertyKey()
                {
                    gid = id,
                    gskid = pv.gskid,
                    GoodPropertyValue = listvalue
                });

                foreach (int vid in pv.values)
                {
                    listvalue.Add(new GoodPropertyValue()
                    {
                        gsvid = vid
                    });
                }
            }

            int num = bll.UpdateGoods(gd);

            Result res = new Result();
            res.code = num > 0 ? 1 : -1;
            res.msg = num > 0 ? "修改成功" : "修改失败";

            return res;
        }

        // DELETE: api/Goods/5
        public void Delete(int id)
        {
        }
    }
}
